package com.borqsprocessmanager.view;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.borqsprocessmanager.App;
import com.borqsprocessmanager.R;

public class ActionPopWindow implements OnTouchListener {

    public static final int TYPE_ONLY_UNINSTILL = 0;
    public static final int TYPE_ONLY_KILLPROCESS = 1;
    public static final int TYPE_UNINSTILL_AND_KILLPROCESS = 2;
    public static final int TYPE_NOROOT_AUTOPROCESS = 3;

    private ActionPopWindow mInstance = null;

    private Activity mActivity;
    private PopupWindow mPopupWindow;
    private View left;
    private View right;
    private TextView dataText;
    private View popView;

    public ActionPopWindow(Activity activity,
            android.view.View.OnClickListener listener) {
        mActivity = activity;
        LayoutInflater mLayoutInflater = (LayoutInflater) App.mContext
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        popView = mLayoutInflater.inflate(R.layout.dialog_action, null);
        popView.setOnTouchListener(this);

        left = popView.findViewById(R.id.dialog_left_layout);
        right = popView.findViewById(R.id.dialog_right_layout);
        dataText = (TextView) left.findViewById(R.id.dialog_action_left_text);

        left.setOnClickListener(listener);
        right.setOnClickListener(listener);

    }

    public void showPop(int type) {
        // mInstance = new ActionPopWindow(activity, listener, type);
        setLayout(type);
        showPop();
    }

    private void showPop() {
        mPopupWindow = new PopupWindow(popView, LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT) {
        };
        mPopupWindow.showAtLocation(mActivity.findViewById(R.id.main_layout),
                Gravity.CENTER, 0, 0);
        mPopupWindow.setAnimationStyle(android.R.style.Animation_Toast);
        mPopupWindow.update();
    }

    private void setLayout(int type) {
        switch (type) {
        case TYPE_ONLY_UNINSTILL:
            right.setVisibility(View.GONE);
            dataText.setText(App.mContext.getText(R.string.action_uninstall));
            break;
        case TYPE_UNINSTILL_AND_KILLPROCESS:
            right.setVisibility(View.VISIBLE);
            dataText.setText(App.mContext.getText(R.string.action_uninstall));
            break;
        case TYPE_NOROOT_AUTOPROCESS:
            right.setVisibility(View.GONE);
            dataText.setText(App.mContext.getText(R.string.action_noroot));
            break;
        default:
            break;
        }
    }

    public void dissmisDialog() {
        dissmis();
    }

    private void dissmis() {
        mPopupWindow.dismiss();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mPopupWindow.dismiss();
        return true;
    }
}
